namespace AForge.Math
{
	using System;

	/// <summary>
	/// Histogram
	/// </summary>
	public class Histogram
	{
		private int[]	values;
		private double	mean = 0;
		private double	stdDev = 0;
		private int		median = 0;
		private int		min;
		private int		max;

        private bool Scale = false;
        private Single ScaleSlope, ScaleIntercept;

		// Get values
		public int[] Values
		{
			get { return values; }
		}
		// Get mean
		public double Mean
		{
			get { return mean; }
		}
		// Get standard deviation
		public double StdDev
		{
			get { return stdDev; }
		}
		// Get median
		public int Median
		{
			get { return median; }
		}
		// Get min value
		public int Min
		{
			get { return min; }
		}
		// Get max value
		public int Max
		{
			get { return max; }
		}

		// Constructor
		public Histogram(int[] values, bool Scale, Single ScaleSlope, Single ScaleIntercept)
		{
			this.values = values;

            this.Scale = Scale;
            this.ScaleSlope = ScaleSlope;
            this.ScaleIntercept = ScaleIntercept;

			int i, n = values.Length;

			max = 0;
			min = n;

			// calculate min and max
            values[0] = 0;
            for (i = 0; i < n; i++) //ignore i = 0
			{
                //scaled values here !!!!!!!!!!!!!
                if (Scale)
                    this.values[i] = (int) (this.values[i] * ScaleSlope + ScaleIntercept);                

				if (this.values[i] != 0)
				{                      
					// max
					if (i > max)
						max = i;
					// min
					if (i < min)
						min = i;
				}
			}

            if (Scale)
                mean = 0;
            else
                mean = Statistics.Mean(this.values);
			stdDev	= Statistics.StdDev(this.values);
			median	= Statistics.Median(this.values);
		}

		// Get range around median containing specified
		// percentile of values
		public Range GetRange(double percent)
		{
			return Statistics.GetRange(values, percent);
		}
	}
}
